package com.hrt.datasender.datacollect.serial;

import java.util.List;

import com.btcode.common.MyLog;
import com.btcode.log.ILog;
import com.hrt.datasender.datadao.model.BcSerialModel;
import com.hrt.datasender.datadao.service.BcSerialService;

public final class SerialLoader {

    private ILog log;

    private static volatile SerialLoader instance;

    private SerialLoader() {
        log = MyLog.getInstance().getLogger(getClass());
    }

    public static SerialLoader getInstance() {
        if (instance == null) {
            synchronized (SerialLoader.class) {
                if (instance == null) {
                    instance = new SerialLoader();
                }
            }
        }
        return instance;
    }

    /**
     * 加载配置好的串口信息
     */
    public void loadSerial() {

        log.info("开始加载串口");

        try {
            List<BcSerialModel> models = BcSerialService.getInstance().getSerials();

            for (BcSerialModel model : models) {

                try {
                    if (!"NONE".equals(model.getComNum())) {
                        SerialDataTransport serialDataTransport = new SerialDataTransport(model);
                        serialDataTransport.init();
                        log.info("加载串口：" + model.getComNum() + "成功");
                    }
                }
                catch (Exception e) {
                    log.error("加载串口出错", e);
                }
            }
        }
        catch (Exception e) {
            log.error("加载串口出错", e);
        }

    }

}
